<?php

namespace App\B2BProject\Custom\Business;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Collection;

/**
 * Class GoodsCategory
 *
 * 商品分类，包含全局分类和店铺分类，两者区别仅在于是否关联店铺
 *
 * @package App\B2BProject\Custom\Business
 */
class GoodsCategory extends Model
{
    protected $fillable = ['name', 'sort', 'display'];

    public function goods()
    {
        return $this->hasMany('App\B2BProject\Custom\Business\Goods', 'category_id', 'id');
    }

    public function allGoods()
    {
        return $this->hasMany('App\B2BProject\Custom\Business\Goods', 'class_id', 'id');
    }

    public function brands()
    {
        return $this->belongsToMany('App\B2BProject\Custom\Business\Brand');
    }

    /**
     * 获取上级分类集合
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function parent()
    {
        return $this->belongsTo('App\B2BProject\Custom\Business\GoodsCategory', 'parent_id', 'id');
    }

    /**
     * 获取下级分类集合
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function children()
    {
        return $this->hasMany('App\B2BProject\Custom\Business\GoodsCategory', 'parent_id', 'id');
    }

    /**
     * 获取所属店铺
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function store()
    {
        return $this->belongsTo('App\B2BProject\Custom\Business\Store', 'store_id', 'id');
    }

    /**
     * 获取全局分类（Master）
     *
     * 若要获取全局分类的子分类（即全局分类的二级分类），可利用关联模型中提供的 children 属性，对其遍历获取
     *
     * @return Collection
     */
    public static function getGlobalCategory()
    {
        $collection = self::where('store_id', 0)->where('display', true)->where('parent_id', 0)->orderBy('sort',
            'desc')->get();

        return $collection;
    }

    /**
     * 根据商家来进行分类查询
     * @param $store_id
     * @return mixed
     */
    public static function getStoreCategory($store_id)
    {
        $collection = self::where('store_id', $store_id)->where('parent_id', 0)->orderBy('sort',
            'desc')->get();

        return $collection;
    }

    public function scopeMasterList(Builder $builder)
    {
        return $builder->where('parent_id', 0);
    }

    /**
     * 查询全局分类（Master）
     *
     * @param $query
     *
     * @return mixed
     */
    public function scopeGlobalCategory($query)
    {
        return $query->where('store_id', 0)->where('display', true)->where('parent_id', 0);
    }

    /**
     * 报盘信息的大分类
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function offerInPrimary()
    {
        return $this->hasMany('App\B2BProject\Custom\Provider\Offer', 'primary_category_id', 'id');
    }

    /**
     * 报盘信息的小分类
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function offerInSecondary()
    {
        return $this->hasMany('App\B2BProject\Custom\Provider\Offer', 'secondary_category_id', 'id');
    }

    /**
     * 询盘信息的大分类
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function enquiryInPrimary()
    {
        return $this->hasMany('App\B2BProject\Custom\Purchaser\Enquiry', 'primary_category_id', 'id');
    }

    /**
     * 询盘信息的小分类
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function enquiryInSecondary()
    {
        return $this->hasMany('App\B2BProject\Custom\Purchaser\Enquiry', 'secondary_category_id', 'id');
    }

    /**
     *  商品的分类
     */
    public static function jsonDataGenerate()
    {
        $cateData[] = [
            'title'  => '全部',
            'value'=>'0',
            'detail' => [
                [
                    'text'  => '全部',
                    'value' => '0'
                ]
            ]
        ];
        foreach (self::getGlobalCategory() as $master) {
            $cateData[] = [

                'title'  => $master->name,
                'value'=>$master->id,
                'detail' => call_user_func_array(function ($master) {
                    $data   = [];
                    $data[] = [
                        'text'  => '全部',
                        'value' => $master->id,
                    ];
                    foreach ($master->children as $child) {
                        $data[] = [

                            'text'  => $child->name,
                            'value' => $child->id
                        ];
                    }

                    return $data;
                }, [$master])
            ];
        }

        return json_encode($cateData);
    }
}
